package com.xhwl.centiotdata.suppliersystem.eesd.zongheng;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.enums.device.DeviceProductCategoryEnum;
import com.xhwl.centiotdata.pojo.enums.eesd.EesdMethodEnum;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.cent.IEesdCentService;
import com.xhwl.centiotdata.service.integrateconfig.EESDSupplierConfig;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.energy.EesdElectricDetailDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import com.xhwl.common.vo.cent.eesd.EesdElectricityWaterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 纵横-伯顿水电表推送
 * @Date: 2023/4/19
 * @Author: hule
 */
@RestController
@Slf4j
public class EesdZhApiHandler {

    @Autowired
    IEesdCentService iEesdCentService;

//    @Autowired
//    private EESDSupplierConfig supplierConfig;

    @Autowired
    private IDeviceCentService deviceCentService;

    /**
     * 对外接口，伯顿推送水电表读数数据
     */
    @PostMapping(value = "/zongheng/electric-water/push")
    public ResultJson pushElectricWater(@RequestPart(value = "vo") EesdElectricityWaterVO vo) {
        List<EesdElectricDetailDTO> dtos = new ArrayList<>();
        EesdElectricDetailDTO dto = new EesdElectricDetailDTO();
        if (vo.getMeterNo() == null || vo.getData().getZxygzdl() == null){
            log.error("伯顿推送水电数据内容为空：{}", vo.toString());
            return ResultJson.fail("推送失败，检测数据为空");
        }
        //从设备模块根据MeterNO查询设备类型，设置进去
        DevicePageQuery devicePageQuery = new DevicePageQuery();
        devicePageQuery.setThirdDeviceId(vo.getMeterNo());
        List<DeviceDTO> deviceDTOS = deviceCentService.listDevice(null, devicePageQuery);
        log.info("伯顿推送查询设备列表：{}", deviceDTOS);
//        List<DeviceProjSupplierCategory> list = supplierConfig.getIntegrationConfig(DeviceProductCategoryEnum.EESD.getId())
//                .stream().filter(c -> c.getProductId().equals(EesdMethodEnum.BODUN.getId())
//                        && !c.getProperties().isEmpty()).collect(Collectors.toList());
        if (deviceDTOS.size() > 0){
            dto.setProjectId(deviceDTOS.get(0).getProjectId());
            dto.setDeviceType(deviceDTOS.get(0).getDeviceTypeId());
        }else{
            log.error("未查询到相关水电表设备信息");
            return ResultJson.fail("推送失败，未查询到相关水电表设备信息");
        }
        dto.setDeviceId(vo.getMeterNo());
        dto.setTime(new Date());
        dto.setRate(0.0);
        dto.setJsonFieldFlag(false);
        EesdElectricDetailDTO.ValueEntity ValueEntity = new EesdElectricDetailDTO.ValueEntity();
        ValueEntity.setValue(Double.valueOf(vo.getData().getZxygzdl()));
        dto.setValue(ValueEntity);
        log.info("伯顿推送数据转换map:{}", JSONObject.toJSONString(dto));
        dtos.add(dto);
        return iEesdCentService.baDeviceDetailSync(dtos);
    }


}
